<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>权限管理</title>
</head>
<body>
    <script>
        const baseData = [
            {name: '精灵云', id: '0'},
            {name: '研发部', id: '0-0', parentkey: '0'},
            {name: '财务部', id: '0-1', parentkey: '0'},
            {name: '质量部', id: '0-2', parentkey: '0'},
            {name: '边缘云业务线', id: '0-0-0', parentkey: '0-0'},
            {name: '大数据业务线', id: '0-0-1', parentkey: '0-0'},
            {name: 'devloops业务线', id: '0-0-2', parentkey: '0-0'},
            {name: '仿真业务线', id: '0-0-3', parentkey: '0-0'},
            {name: '审计部', id: '0-1-0', parentkey: '0-1'},
            {name: '会计部', id: '0-1-1', parentkey: '0-1'},
            {name: '测试部', id: '0-2-0', parentkey: '0-2'},
            {name: '运维部', id: '0-2-1', parentkey: '0-2'},
        ]

        const deepClone = target => {
            let result
            if(typeof target !== 'object') return target
            if(Array.isArray(target)) {
                result = []
                target.forEach(item => {
                    result.push(deepClone(item))
                })
            }else {
                result = {}
                for(let i in target) {
                    result[i] = deepClone(target[i])
                }
            }
            return result
        }

        let cloneData = deepClone(baseData)

        // 将对象数组转换成一棵树
        let treeList = []
        const transferTree = list => {
            list.forEach((item ,i) => {
                item.children = []
                if(!item.parentkey) {
                    treeList.push(item)
                }
                list.forEach((item1, j) => {
                    if(item.id === item1.parentkey) {
                        item.children.push(item1)
                    }
                })

            })
            return treeList
        }

        treeList = transferTree(cloneData)
        console.log(treeList);

        // 将树扁平化
        let treeFlatArr = []
        const flatTree = tree => {
            tree.forEach((item, i) => {
                if(!item.parentkey) {
                    treeFlatArr.push(item)
                    flatTree(item.children)
                }
                if(item.parentkey) {
                    treeFlatArr.push(item)
                    if(item.children.length !== 0) {
                        flatTree(item.children)
                    }
                }
            })
            return treeFlatArr
        }

        treeFlatArr = flatTree(treeList)
        console.log(treeFlatArr);

        // 获取id数据
        let idArr = []
        const getId = (list, id) => {
            idArr.push(id)
            list.forEach((item, i) => {
                if(id === item.id) {
                    if(item.parentkey) {
                        getId(list, item.parentkey)
                    }
                }
            })
            return idArr
        }

        // 获取传入数组型的ids
        let totalArr = []
        const getIdArr = (list, ids) => {
            ids.forEach((item, i) => {
                idArr = []
                totalArr.push(getId(list, item))

            })
            console.log(totalArr);
        }
        getIdArr(treeFlatArr, ['0-2-1', '0-2', '0-0-1'])
    </script>
</body>
</html>